import { Routes } from '../routes/crmRoutes';
import express = require('express');
import bodyParser = require('body-parser');
import mongoose from 'mongoose';

class App {
    public app: express.Application;
    public routePrv: Routes = new Routes();
    private mongoUrl: string = 'mongodb://localhost/CRMdb'; // 直接链接数据库

    constructor() {
        this.app = express();
        this.config();
        this.routePrv.routes(this.app);
        this.mongoSetup();
    }

    // 中间件
    private config() {
        // support application/json type post data;
        this.app.use(bodyParser.json());

        // support application/x-www-form-urlencoded post data;
        this.app.use(bodyParser.urlencoded({ extended: false }));
    }

    private mongoSetup(): void {
        mongoose.connect(this.mongoUrl, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: false
        });
    }
}

export default new App().app; // default
